package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomListActivity extends Activity {

    private static final String TAG = "CustomList";
    private static final int EXCHANGE_RATE_REQUEST = 1;  // 请求码

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_list);

        ListView mylist2 = findViewById(R.id.mylist2);

        // 创建handler用于处理获取到的数据
        handler = new Handler(msg -> {
            if (msg.what == 7) {
                List<HashMap<String, String>> listItems = (List<HashMap<String, String>>) msg.obj;

                // 使用SimpleAdapter来绑定数据
                SimpleAdapter adapter = new SimpleAdapter(
                        CustomListActivity.this,
                        listItems,
                        R.layout.list_item,
                        new String[]{"Currency", "ExchangeRate"},
                        new int[]{R.id.itemTitle, R.id.itemDetail} // 视图id
                );
                mylist2.setAdapter(adapter);

                // 设置点击事件，点击条目跳转到Exchange页面
                mylist2.setOnItemClickListener((parent, view, position, id) -> {
                    String selectedCurrency = listItems.get(position).get("Currency");
                    String selectedRate = listItems.get(position).get("ExchangeRate");

                    // 启动 CurrencyExchangeActivity，并传递选中的币种和汇率
                    Intent intent = new Intent(CustomListActivity.this, CurrencyExchangeActivity.class);
                    intent.putExtra("currency", selectedCurrency);
                    intent.putExtra("rate", selectedRate);
                    startActivityForResult(intent, EXCHANGE_RATE_REQUEST);
                });
            }
            return true;
        });

        // 开启线程来获取和解析网页数据
        new Thread(() -> {
            List<HashMap<String, String>> retList = new ArrayList<>();
            try {
                Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();

                Elements tables = doc.getElementsByTag("table");
                Element table = tables.get(1);
                Elements trs = table.getElementsByTag("tr");

                trs.remove(0);

                for (Element tr : trs) {
                    Elements tds = tr.children();
                    String currency = tds.get(0).text();
                    String exchangeRate = tds.get(5).text();

                    // 将数据添加到列表中
                    HashMap<String, String> map = new HashMap<>();
                    map.put("Currency", currency);
                    map.put("ExchangeRate", exchangeRate);
                    retList.add(map);
                }

                // 更新UI
                Message msg = handler.obtainMessage();
                msg.what = 7;
                msg.obj = retList;
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
